#include "led.h"
#include "drv_common_def.h"
#include "drv_ed_utility.h"
#include "shell.h"

static EvtState BlinkyEvt(Event * const evt)
{
    evt->exeState++;
    if (evt->exeState <= 4) {
        BrdLed_Switch();
    }
    if (evt->exeState >= 10) {
        evt->exeState = 0;
    }
    return ED_TRAN(EVT_HANDLED);
}

static uint32_t g_blinkSN = 0;
void BlinkyInit(void)
{
    if (g_blinkSN == 0) {
        g_blinkSN = ED4E_PublishPeriodicEvent(BlinkyEvt, 100);
    }
}

static void ShellCmd_KLedEvt_Test(const ShellCmdArgs *args)
{
    const char *arg = args->getArg(0);
    if (arg == NULL) {
        return;
    }
    switch (*arg) {
    case '0':
        ED4E_SetPeriodicEvent(g_blinkSN, EVT_PERIOD_PAUSE);
        break;
    case '1':
        ED4E_SetPeriodicEvent(g_blinkSN, EVT_PERIOD_RESUME);
        break;
    case '2':
        ED4E_SetPeriodicEvent(g_blinkSN, EVT_KILL);
        g_blinkSN = 0;
        break;
    case '3':
        BlinkyInit();
        break;
    default:
        break;
    }
}
EXPORT_SHELL_CMD("led", ShellCmd_KLedEvt_Test);